package comWorld.user.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.gjs.tools.utils.Utils;

import comWorld.action.blog.BlogSave;
import comWorld.blog.BlogInfoManage;
import comWorld.blog.BlogLog;
import comWorld.social.SocialInfo;
import comWorld.social.SocialManage;
import comWorld.social.VisitorCount;
import comWorld.user.UserInfo;
import comWorld.user.UserStatus;
import comWorld.user.manage.UserInfoManage;

/**
 * Servlet implementation class UserHomePage
 */
@WebServlet("/userHomePage")
public class UserHomePage extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserHomePage() {
        super();
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserStatus userStatus = (UserStatus) request.getSession().getAttribute("visitorStatus");
		SocialInfo socialInfo = (SocialInfo) request.getAttribute("socialInfo");
		int userId = (int) request.getAttribute("userId");
		
		VisitorCount visitorCount = VisitorCount.getLastVisitorCount(userId);
		visitorCount.add();
		request.setAttribute("visitorCount", visitorCount);
		
		request.setAttribute("userContactInfo", UserInfoManage.getUserContactInfo(userId));
		
		String ft = request.getParameter("ft");
		ft = (ft == null || ft.length() == 0) ? "all" : ft;
		request.setAttribute("ft", ft);
		int privacy = 0;
		if (userStatus.getId() != userId)
			privacy = 1;
		List<BlogLog> blogLogs = BlogInfoManage.getBlogLogs(userId, ft, privacy);
		
		int[] be = Utils.paging(request, 10, blogLogs.size());
		
		request.setAttribute("blogLogs", blogLogs.subList(be[0], be[1]));
		
		String forward = "/user/userHomePage.jsp";
		if (!userStatus.isLogin() || userStatus.getId() != userId) {
			request.setAttribute("followed", SocialManage.judgeUserHasFollowed(userStatus, socialInfo, userId));
			forward = "/user/otherUserHomePage.jsp";
		}
		request.getRequestDispatcher(forward).forward(request, response);
	}

}
